<?php
/**
 * 品牌购
 */
App::import ( 'Vendor', 'Taobaotop/TopClient' );
App::import ( 'Vendor', 'Taobaotop/RequestCheckUtil' );
App::import ( 'Vendor', 'Taobaotop/Logger' );
class ShopController extends AppController {
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'shop.css',
				'shopcate.css' 
		) );
		
		$this->loadModel ( 'GoodsCate' );
		// 服装分类
		$conditions = array (
				'parentid' => 1,
				'status' => 1 
		);
		$fuzhuang_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		
		$this->set ( 'fuzhuang_list', $fuzhuang_list );
		
		// 配饰
		$conditions = array (
				'parentid' => 4,
				'status' => 1 
		);
		$peishi_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'peishi_list', $peishi_list );
		
		// 鞋子
		$conditions = array (
				'parentid' => 2,
				'status' => 1 
		);
		$xiezi_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'xiezi_list', $xiezi_list );
		
		// 包袋
		$conditions = array (
				'parentid' => 3,
				'status' => 1 
		);
		$baobao_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'baobao_list', $baobao_list );
		
		// 居家
		$conditions = array (
				'parentid' => 6,
				'status' => 1 
		);
		$jujia_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'jujia_list', $jujia_list );
		
		// 美容
		$conditions = array (
				'parentid' => 7,
				'status' => 1 
		);
		$meirong_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'meirong_list', $meirong_list );
		
		// 手机数码
		$conditions = array (
				'parentid' => 8,
				'status' => 1 
		);
		$shuma_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'shuma_list', $shuma_list );
		
		// 百货
		$conditions = array (
				'parentid' => 6,
				'status' => 1 
		);
		$baihuo_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'baihuo_list', $baihuo_list );
		
		// 美食
		$conditions = array (
				'parentid' => 5,
				'status' => 1 
		);
		$meishi_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'meishi_list', $meishi_list );
		
		// 图书音像
		$conditions = array (
				'parentid' => 14,
				'status' => 1 
		);
		$tushu_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'tushu_list', $tushu_list );
		
		// 文化娱乐
		$conditions = array (
				'parentid' => 15,
				'status' => 1 
		);
		$yule_list = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'yule_list', $yule_list );
	}
	
	/**
	 * 品牌购首页
	 */
	public function index() {
		$this->set ( 'title_for_layout', '女人集市 - 品牌购' );
		
		// 每日精品推荐
		$this->loadModel ( "Goods" );
		$conditions = array ();
		$list = $this->Goods->GetGoodsRecommend ( $conditions, 20 );
		
		$this->set ( 'shop_list', $list );
	}
	/**
	 * 全部商品
	 */
	public function all() {
		$this->set ( 'title_for_layout', '女人集市 - 品牌购' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array ();
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 分类产品列表
	 */
	public function item() {
		$this->set ( 'title_for_layout', '女人集市 - 潮流服装,时尚服装搭配' );
		
		// 获取商品内容
		
		// 接收分页参数
		$id = $this->params ['id'];
		
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $id 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		$this->set ( 'id', $id );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 服装
	 */
	public function fuzhuang() {
		$this->set ( 'title_for_layout', '女人集市 - 潮流服装,时尚服装搭配' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 1 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 配饰
	 */
	public function peishi() {
		$this->set ( 'title_for_layout', '女人集市 - 配饰' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 4 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 鞋子
	 */
	public function xiezi() {
		$this->set ( 'title_for_layout', '女人集市 - 鞋子' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 2 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 包袋
	 */
	public function baobao() {
		$this->set ( 'title_for_layout', '女人集市 - 包袋' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 3 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 居家
	 */
	public function jujia() {
		$this->set ( 'title_for_layout', '女人集市 - 居家' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 6 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 美容
	 */
	public function meirong() {
		$this->set ( 'title_for_layout', '女人集市 - 美容' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 7 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 手机数码
	 */
	public function shuma() {
		$this->set ( 'title_for_layout', '女人集市 - 手机数码' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 8 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 百货
	 */
	public function baihuo() {
		$this->set ( 'title_for_layout', '女人集市 - 百货' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 6 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 美食
	 */
	public function meishi() {
		$this->set ( 'title_for_layout', '女人集市 - 美食' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 5 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 图书音像
	 */
	public function tushu() {
		$this->set ( 'title_for_layout', '女人集市 - 图书音像' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 14 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 文化娱乐
	 */
	public function yule() {
		$this->set ( 'title_for_layout', '女人集市 - 文化娱乐' );
		
		// 获取商品内容
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		$this->loadModel ( "GoodsCate" );
		
		$ids = $this->GoodsCate->getGoodsCateList ( 15 );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'cid' => $ids 
		);
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		$this->set ( 'shop_list', $list ['result'] );
		
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 显示产品详细
	 */
	public function show() {
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'show.css' 
		) );
		
		$id = $this->params ['id'];
		
		$this->loadModel ( 'Goods' );
		
		$Goodsinfo = $this->Goods->GetGoodsByID ( $id );
		if (! empty ( $Goodsinfo ['Goods'] ['price'] )) {
			// 用户信息
			$this->loadModel ( "User" );
			$UserInfo = $this->User->GetUserByID ( $Goodsinfo ['Goods'] ['uid'] );
			if ($UserInfo) {
				if (empty ( $UserInfo ['User'] ['head'] )) {
					$UserInfo ['User'] ['head'] = "/img/dfhead.gif";
				}
			}
			$this->set ( 'UserInfo', $UserInfo );
			
			// 专辑信息
			$this->loadModel ( "UserAlbum" );
			$UserAlbum = $this->UserAlbum->GetUserAlbumByID ( $Goodsinfo ['Goods'] ['aid'] );
			$this->set ( 'UserAlbum', $UserAlbum );
			
			$price_arr = explode ( '.', $Goodsinfo ['Goods'] ['price'] );
			$Goodsinfo ['Goods'] ['price_block'] = $price_arr [0];
			$Goodsinfo ['Goods'] ['price_points'] = $price_arr [1];
			
			$img_arr = explode ( "_", $Goodsinfo ['Goods'] ['img'] );
			$ext = explode ( ".", $img_arr [1] );
			
			$Goodsinfo ['Goods'] ['img_80'] = "{$img_arr[0]}_80.{$ext[1]}";
			$Goodsinfo ['Goods'] ['img_100'] = "{$img_arr[0]}_100.{$ext[1]}";
			$Goodsinfo ['Goods'] ['img_200'] = "{$img_arr[0]}_200.{$ext[1]}";
			$Goodsinfo ['Goods'] ['img_460'] = "{$img_arr[0]}_460.{$ext[1]}";
			
			// 折扣计算
			$Goodsinfo ['Goods'] ['discount'] = (1 - round ( ($Goodsinfo ['Goods'] ['old_price'] - $Goodsinfo ['Goods'] ['price']) / $Goodsinfo ['Goods'] ['old_price'], 2 )) * 10;
		}
		$this->set ( 'Goodsinfo', $Goodsinfo );
		
		$this->set ( 'title_for_layout', "女人集市 - {$Goodsinfo['Goods']['title']}" );
	}
	
	/**
	 * 商品跳转
	 */
	public function jump() {
		// 不使用模板
		$this->autoRender = false;
		
		$id = $this->params ['id'];
		
		$this->loadModel ( 'Goods' );
		
		$Goodsinfo = $this->Goods->GetGoodsByID ( $id );
		
		// 淘宝客PID
		// $taobao_app_pid = Configure::read ( 'config.taobao_app_pid' );
		// $taobao_app_key = Configure::read ( 'config.taobao_app_key' );
		// $taobao_app_secret = Configure::read ( 'config.taobao_app_secret' );
		
		$url = $Goodsinfo ['Goods'] ['url']; // 原地址
		$click_url = $Goodsinfo ['Goods'] ['click_url']; // 转换后的地址
		
		header ( "Location: {$click_url}" );
		exit ();
	}
}
?>